<template>
  <el-dialog
    title="待审核"
    :model-value="modelValue"
    @close="closed"
    width="800px"
    append-to-body
  >
    <el-form ref="recordRef" :model="workOrderForm" label-width="160px">
      <el-form-item label="作业类型" prop="jobType">
        <el-select
          v-model="workOrderForm.jobType"
          placeholder="作业类型"
          clearable
          style="width: 100%"
          :disabled="true"
        >
          <el-option
            v-for="dict in dl_job_type"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="工单编号" prop="processOrderNo">
        <el-input
          v-model="workOrderForm.processOrderNo"
          placeholder=""
          :disabled="true"
        />
      </el-form-item>

      <el-form-item label="工作负责人" prop="workHeaderName">
        <el-input
          v-model="workOrderForm.workHeaderName"
          placeholder=""
          :disabled="true"
        />
      </el-form-item>

      <el-form-item label="作业组织" prop="jobGroupName">
        <el-tree-select
          v-model="workOrderForm.jobGroupName"
          :data="deptOptions"
          :props="{ value: 'label', label: 'label', children: 'children' }"
          value-key="label"
          placeholder="请选择"
          check-strictly
          :disabled="true"
          style="width: 100%"
        />
      </el-form-item>

      <el-form-item label="作业风险等级" prop="jobRiskLevel">
        <el-select
          v-model="workOrderForm.jobRiskLevel"
          placeholder="作业风险等级"
          clearable
          :disabled="true"
          style="width: 100%"
        >
          <el-option
            v-for="dict in dl_job_risk_level"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="计划检修时间" prop="planStartDate">
        <el-date-picker
          clearable
          v-model="workOrderForm.planStartDate"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
          placeholder=""
          :disabled="true"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="电网风险等级" prop="powerGridRiskLevel">
        <el-select
          v-model="workOrderForm.powerGridRiskLevel"
          placeholder="电网风险等级"
          clearable
          :disabled="true"
          style="width: 100%"
        >
          <el-option
            v-for="dict in dl_power_grid_risk_level"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="计划状态" prop="planStatus">
        <el-select
          v-model="workOrderForm.planStatus"
          placeholder="计划状态"
          clearable
          :disabled="true"
          style="width: 100%"
        >
          <el-option
            v-for="dict in dl_plan_status"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="电压等级" prop="voltageLevel">
        <el-select
          v-model="workOrderForm.voltageLevel"
          placeholder="电压等级"
          clearable
          :disabled="true"
          style="width: 100%"
        >
          <el-option
            v-for="dict in dl_voltage_level"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="作业内容">
        <div class="ql-editor" v-html="workOrderForm.iobContent"></div>
      </el-form-item>

      <el-form-item label="是否异动" prop="isChange">
        <el-select
          v-model="workOrderForm.isChange"
          placeholder="是否异动"
          clearable
          :disabled="true"
          style="width: 100%"
        >
          <el-option
            v-for="dict in dl_ischange"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="异动内容">
        <el-input
          v-model="workOrderForm.changeContent"
          type="textarea"
          placeholder="请输入"
          :disabled="true"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="reviewForm">审 核</el-button>
        <el-button type="primary" @click="backForm">退 回</el-button>
        <el-button type="primary" @click="stopForm">终 止</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
    <div>
      <reviewDialog
        v-model="reviewVisible"
        @review="handleReview"
      ></reviewDialog>
    </div>
  </el-dialog>
</template>

<script setup>
import {
  updateOrder,
  reviewOrder,
  reviewBackOrder,
  processReviewStopOrder,
} from "@/api/platform/order";

import { deptTreeSelect } from "@/api/system/user";

import reviewDialog from "@/views/workOrder/waitingToDo/dialog/reviewDialog.vue";

const { proxy } = getCurrentInstance();
const {
  dl_voltage_level,
  dl_job_risk_level,
  dl_power_grid_risk_level,
  dl_job_type,
  dl_plan_status,
  dl_sfsjyhbg,
  dl_sfsjdybg,
  dl_ischange,
} = proxy.useDict(
  "dl_voltage_level",
  "dl_job_risk_level",
  "dl_power_grid_risk_level",
  "dl_job_type",
  "dl_plan_status",
  "dl_sfsjyhbg",
  "dl_sfsjdybg",
  "dl_ischange"
);

const deptOptions = ref(undefined);

const emits = defineEmits(["update:modelValue", "refresh"]);

const props = defineProps({
  workOrderForm: {
    type: Object,
  },
  modelValue: {
    type: Boolean,
    required: true,
  },
});

/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then((response) => {
    deptOptions.value = response.data;
  });
}

getDeptTree();

const reviewVisible = ref(false);

const reviewForm = () => {
  reviewVisible.value = true;
};

const backForm = () => {
  props.workOrderForm.nextHandleUser = props.workOrderForm.applyUser;
  props.workOrderForm.nextHandleUserName = props.workOrderForm.applyUserName;
  reviewBackOrder(props.workOrderForm).then((response) => {
    proxy.$modal.msgSuccess("回退成功");
    emits("update:modelValue", false);
    emits("refresh");
  });
};

const stopForm = () => {
  processReviewStopOrder(props.workOrderForm).then((response) => {
    proxy.$modal.msgSuccess("终止成功");
    emits("update:modelValue", false);
    emits("refresh");
  });
};

const handleReview = (row) => {
  if (row.isPass === "是") {
    props.workOrderForm.nextHandleUser = row.handleUser;
    props.workOrderForm.nextHandleUserName = row.handleUserName;
    props.workOrderForm.processStatus = 5;
    props.workOrderForm.handleRemark = row.reviewContent
  } else {
    props.workOrderForm.nextHandleUser = props.workOrderForm.applyUser;
    props.workOrderForm.nextHandleUserName = props.workOrderForm.applyUserName;
    props.workOrderForm.processStatus = 3;
    props.workOrderForm.handleRemark = row.reviewContent
  }
  reviewOrder(props.workOrderForm).then((response) => {
    proxy.$modal.msgSuccess("审核成功");
    emits("update:modelValue", false);
    emits("refresh");
  });
};

const cancel = () => {
  emits("update:modelValue", false);
};
</script>
